package zeebe.object;

import io.zeebe.client.ZeebeClient;
import io.zeebe.client.api.response.ActivatedJob;
import io.zeebe.client.api.worker.JobClient;
import io.zeebe.client.api.worker.JobHandler;

import java.util.HashMap;
import java.util.Map;

public class InputJobWorkerHandler implements JobHandler {
    ZeebeClient zeebeClient;

    public InputJobWorkerHandler(ZeebeClient client) {
        this.zeebeClient = client;
    }

    @Override
    public void handle(JobClient jobClient, ActivatedJob activatedJob) throws Exception {

        Corporation corporation = activatedJob.getVariablesAsType(Corporation.class);
        System.out.println("input的企业名:" + corporation.getTitle());
        System.out.println("input的人名:" + corporation.getPerson().getName());

        jobClient.newCompleteCommand(activatedJob.getKey())
                .send()
                .join();

        Map messageMap = new HashMap();
        String correlationKey = "inputMsg";
        messageMap.put(correlationKey, correlationKey);
        zeebeClient.newSetVariablesCommand(activatedJob.getWorkflowInstanceKey())
                .variables(messageMap)
                .send()
                .join();

        zeebeClient.newPublishMessageCommand()
                .messageName(correlationKey)
                .correlationKey(correlationKey)
                .send()
                .join();
    }
}
